package top.xunfong.knowledge.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.lke.v20231130.models.CreateSharedKnowledgeResponse;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import top.xunfong.common.core.domain.dto.BatchDTO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import top.xunfong.common.core.exception.ServiceException;
import top.xunfong.knowledge.mapper.KnowledgeInfoMapper;
import top.xunfong.knowledge.service.KnowledgeInfoService;
import top.xunfong.knowledge.entity.KnowledgeInfo;
import top.xunfong.knowledge.dto.KnowledgeInfoDTO;
import top.xunfong.knowledge.dto.KnowledgeInfoQueryDTO;
import top.xunfong.knowledge.service.tencent.TencentApiService;
import top.xunfong.knowledge.vo.KnowledgeInfoVO;
import top.xunfong.mybatis.core.page.PageDTO;
import top.xunfong.common.core.domain.R;
import top.xunfong.common.core.domain.TableDataInfo;
import org.springframework.stereotype.Service;
import top.xunfong.satoken.util.LoginHelper;

import java.util.List;

@Service
@Slf4j
public class KnowledgeInfoServiceImpl extends ServiceImpl<KnowledgeInfoMapper, KnowledgeInfo> implements KnowledgeInfoService {

    @Resource
    private TencentApiService tencentApiService;

    /**
     * 新增
     *
     * @param dto
     * @return
     */
    @Override
    public R<?> insertKnowledgeInfo(KnowledgeInfoDTO dto) {
        KnowledgeInfo entity = BeanUtil.copyProperties(dto, KnowledgeInfo.class, "id");
        entity.setUid(LoginHelper.getLoginUserInfo().getUserId());
        save(entity);
        try {
            CreateSharedKnowledgeResponse sharedKnowledge = tencentApiService.createSharedKnowledge(entity.getKname(), entity.getDescription());
            entity.setKid(sharedKnowledge.getKnowledgeBizId());
            entity.setStatus(1);
        } catch (TencentCloudSDKException e) {
            log.error(e.getMessage());
            entity.setStatus(2);
            throw new ServiceException("创建知识库失败");
        }
        updateById(entity);
        return R.ok();
    }


    /**
     * 修改
     *
     * @param dto
     * @return
     */
    @Override
    public R<?> updateKnowledgeInfo(KnowledgeInfoDTO dto) {
        KnowledgeInfo entity = BeanUtil.copyProperties(dto, KnowledgeInfo.class);
        return updateById(entity) ? R.ok() : R.fail();
    }

    /**
     * 批量删除
     *
     * @param dto
     * @return
     */
    @Override
    public R<?> deleteKnowledgeInfo(BatchDTO<Long> dto) {
        List<Long> ids = dto.getData();
        if (CollUtil.isNotEmpty(ids)) {
            return removeByIds(ids) ? R.ok() : R.fail();
        }
        return R.ok();
    }

    /**
     * 列表查询
     *
     * @param dto
     * @return
     */
    @Override
    public TableDataInfo<KnowledgeInfoVO> listKnowledgeInfo(KnowledgeInfoQueryDTO dto, PageDTO pageDTO) {
        Page<KnowledgeInfo> page = pageDTO.build();
        Page<KnowledgeInfo> pageData = baseMapper.selectPage(page, new QueryWrapper<>());
        List<KnowledgeInfo> records = pageData.getRecords();
        TableDataInfo.TableDataInfoBuilder<KnowledgeInfoVO> builder = TableDataInfo.builder();
        builder.total(pageData.getTotal());
        if (CollUtil.isNotEmpty(records)) {
            List<KnowledgeInfoVO> knowledgeInfoVOS = BeanUtil.copyToList(records, KnowledgeInfoVO.class);
            builder.rows(knowledgeInfoVOS);
        }
        return builder.build();
    }

    /**
     * 单查询
     *
     * @param id
     * @return
     */
    @Override
    public R<KnowledgeInfoVO> singleKnowledgeInfo(Long id) {
        KnowledgeInfo byId = this.getById(id);
        return R.ok(BeanUtil.copyProperties(byId, KnowledgeInfoVO.class));
    }
}